package com.example.bbs.service;

import com.example.bbs.bean.CommentPlus;
import com.example.bbs.bean.NotePlus;
import com.example.bbs.bean.User;
import com.example.bbs.http.bean.comment.CommentShow;
import com.example.bbs.http.bean.note.NoteAll;
import com.example.bbs.http.bean.note.NoteSimple;
import com.example.bbs.http.bean.user.UserShowSimple;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
/**
 * 传输对象转换器
 */
public class PaserService {

    protected List<NoteSimple> notePlusListTransform(List<NotePlus> notePlusList) {
        List<NoteSimple> noteSimples = new ArrayList<>();
        for (NotePlus notePlus : notePlusList) {
            noteSimples.add(notePlusTransform(notePlus));
        }
        return noteSimples;
    }

    protected NoteSimple notePlusTransform(NotePlus notePlus) {
        NoteSimple noteSimple = new NoteSimple();
        return notePlusTransform(notePlus, noteSimple);
    }

    private NoteSimple notePlusTransform(NotePlus notePlus, NoteSimple noteSimple) {
        noteSimple.setAuthor(userTransform(notePlus.getCreateby()));
        noteSimple.setGood(notePlus.getIsgood() == 1);
        noteSimple.setTop(notePlus.getTop() == 1);
        noteSimple.setTittle(notePlus.getNotename());
        noteSimple.setNoteId(notePlus.getNoteid());
        noteSimple.setTittleColor(notePlus.getTittlecolor());
        return noteSimple;
    }

    protected UserShowSimple userTransform(User user) {
        UserShowSimple userShowSimple = new UserShowSimple();
        userShowSimple.setUserid(user.getUserid());
        userShowSimple.setHeadimage(user.getHeadimage());
        userShowSimple.setNickname(user.getNickname());
        return userShowSimple;
    }

    protected NoteAll noteTransform(NotePlus notePlus, List<CommentPlus> comments) {
        NoteAll noteAll = new NoteAll();
        noteAll.setText(notePlus.getNote());
        noteAll.setComments(commentListTransform(comments));
        return (NoteAll) notePlusTransform(notePlus, noteAll);
    }

    protected CommentShow commentTransform(CommentPlus commentPlus) {
        CommentShow commentShow = new CommentShow();
        commentShow.setHtml(commentPlus.getCommenttext());
        commentShow.setCommentid(commentPlus.getNotecommentid());
        commentShow.setAuthor(userTransform(commentPlus.getCreateby()));
        return commentShow;
    }

    protected List<CommentShow> commentListTransform(List<CommentPlus> comments) {
        List<CommentShow> commentShows = new ArrayList<>();
        for (CommentPlus commentPlus : comments) {
            commentShows.add(commentTransform(commentPlus));
        }
        return commentShows;
    }
}
